app.controller('RtResourceCtrl', function ($scope, $filter, ajax, $stateParams, toaster, page, $modal, $state) {

    var inserter = new Req.POST({ resource: $scope.resource });
    var deleter = new Req.DELETE({ resource: $scope.resource });
    var updater = new Req.PUT({ resource: $scope.resource });

    $scope.queryMap = {};
    if ($stateParams.group) $scope.queryMap.groupId = Number($stateParams.group);

    $scope.group = null;
    $scope.route = { both: "", forward: "", backward: "", id1: 0, id2: 0 };
    $scope.selected = {};
    $scope.day = $filter('date')(new Date(), 'yyyyMMdd');

    $scope.reload = $scope.page.loader('res/rt/{pageIndex}/{pageSize}/page');
    $scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });

    $scope.query = function () {
        if ($scope.queryMap) {
            if ($scope.selected.obj) {
                $scope.queryMap.routeId = $scope.selected.obj.route.id;
            } else {
                delete $scope.queryMap.routeId;
            }
        }
        $scope.reload(1, $scope.queryMap);
    };

    (function () {
        ajax.call(new Req.GET({
            resource: 'route/rt/' + $scope.queryMap.groupId + '/group'
        })).then(function (result) {
            $scope.group = result.result;
            if ($scope.group) {
                var city1 = $scope.group.route1.city1.name;
                var city2 = $scope.group.route1.city2.name;
                var direction = "{0} ➡ {1}";
                $scope.route.both = "（" + city1 + " ↔ " + city2 + "）";
                $scope.route.forward = direction.format(city1, city2);
                $scope.route.backward = direction.format(city2, city1);
                $scope.route.id1 = $scope.group.route1.id;
                $scope.route.id2 = $scope.group.route2.id;
                $scope.selected.origin = [
                    { route: $scope.group.route1, name: $scope.route.forward },
                    { route: $scope.group.route2, name: $scope.route.backward }
                ];
                $scope.query();
            }
        }, function () { });
    })();

    /** 弹框 */
    $scope.write = function (size, current_data, submit_type) {
        $scope.current_data = angular.copy(current_data);
        $modal.open({
            templateUrl: 'template/rent/tickets/writeModal.html?v=071899c9df',
            controller: 'ResourceModalCtrl',
            size: size,
            resolve: {
                param: function () {
                    return {
                        "group": $scope.group,
                        "data": $scope.current_data,
                        "type": submit_type
                    };
                }
            }
        }).result.then(function (param) {
            if (param.type == 'update') {
                updater.fields = param.data;
                updater.id = param.data.id;
                updater.resource = 'res/rt/update';
                $scope.page.mod(updater, $scope.reload, $scope.queryMap);
            } else if (param.type == 'insert') {
                inserter.datalist = param.datalist;
                inserter.fields = param.data;
                inserter.resource = 'res/rt/insert';
                $scope.page.add(inserter, $scope.reload, $scope.queryMap);
            }
        });
    };

    $scope.del = function (size, current_data, submit_type) {
        var modalInstance = $scope.delModal(size, current_data, submit_type);
        modalInstance.result.then(function (param) {
            deleter.id = param.data.id;
            deleter.resource = 'res/rt/' + deleter.id + '/delete';
            $scope.page.del(deleter, $scope.queryMap);
        });
    };

    $scope.isFree = function (res) { return res.status == 1 || res.status == 5; };

    $scope.activate = function (res) {
        inserter.fields = res;
        inserter.id = res.id;
        inserter.resource = 'res/rt/' + res.id + '/activate';
        $scope.page.mod(inserter, $scope.reload, $scope.queryMap);
    };

    $scope.stopFlag = function (res) {
        res.stopFlag = true;
        updater.fields = res;
        updater.id = res.id;
        updater.resource = 'res/rt/update';
        $scope.page.mod(updater, $scope.reload, $scope.queryMap);
    };

    $scope.back = function () {
        if ($state.previous && $state.previous.name)
            $state.go($state.previous.name, $state.previous.params);
        else $state.go('app.rent.tickets');
    };

});

app.filter('statusFilter', function () {
    return function (status) {
        switch (status) {
            case 1:
                return '未激活';
            case 2:
                return '已激活';
            case 3:
                return '已排班';
            case 4:
                return '运行中';
            case 5:
                return '暂停使用';
            default:
                return '';
        }
    };
});
